﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.BLL.Helper;
using MS.InformationManagement.DAL.RecordCenter;
using MS.InformationManagement.Core.RecordSiteQuery;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Support;
using Microsoft.SharePoint;
using MS.InformationManagement.DAL.DTOs;

namespace MS.InformationManagement.BLL.RequestInformation
{
    public class RequestInformation
    {

        public void RequestInform(int[] documentsId, Guid sourceList, List<string> targetUser)
        {
            var action = new Core.Actions.InformationAction(documentsId, sourceList);
            foreach (var currentUser in targetUser)
            {
                action.Execute(currentUser);
            }
        }

        public List<UserDTO> GetUsers()
        {
            string userSectionName = PeopleHelper.GetPropertyUserProfile(GlobalConstants.UP_DEPARTMENT, SPContext.Current.Web.CurrentUser.LoginName);
            List<UserDTO> users = RecursiveHelper.IsUserSectionAdmin(SPContext.Current.Web.CurrentUser.Name)
                ? GetUsersForAdminUser(userSectionName)
                : GetUsersForStandardUser(userSectionName);
            return users;
        }

        private List<UserDTO> GetUsersForAdminUser(string sectionName)
        {
            Seccion currentSection = GetUserSection(sectionName);
            Dictionary<string, string> result =  PeopleHelper.GetUsersByProperty(GlobalConstants.UP_DEPARTMENT, sectionName, false);
            List<UserDTO> users = (from user in result select new UserDTO { LoginName = user.Key, DisplayName = user.Value }).ToList();
            RecursiveHelper.PurgeUsers(ref users, GlobalConstants.ROLE_INFORM);
            RecursiveHelper.AddSectionAdminUsers(ref users, false);
            return users;
        }

        private List<UserDTO> GetUsersForStandardUser(string sectionName)
        {
            Seccion currentSection = GetUserSection(sectionName);
            Dictionary<string, string> result = PeopleHelper.GetUsersByProperty(GlobalConstants.UP_DEPARTMENT, sectionName, false);
            List<UserDTO> users = (from user in result select new UserDTO { LoginName = user.Key, DisplayName = user.Value }).ToList();
            RecursiveHelper.PurgeUsers(ref users, GlobalConstants.ROLE_INFORM);
            string sectionOwnerName = currentSection.UsuarioEncargadoCorrespondencia;
            RecursiveHelper.AddSectionOwner(ref users, sectionOwnerName);
            return users;
        }

        private Seccion GetUserSection(string section)
        {
            string caml = String.Format("<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></Eq></Where>", section);
            Seccion userSection = QueryRecordsSiteHelper<Seccion>.QueryRecordsEntitiesSite(GlobalConstants.LIST_BIBLIOTECA_SECCION, caml, new Nullable<uint>()).FirstOrDefault();
            if (userSection == default(Seccion)) { throw new Exception(string.Format("La sección {0} no se encuentra", section)); }
            return userSection;
        }

    }
}
